<template>
  <view>
    <view class="goods-list">
      <view v-for="(item,i) in goodsList" :key='i' @click='gotoDetail(item)'>
        <my-goods :goods='item'></my-goods>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        // 获取列表需要的配置对象
        queryObj : {
          query: '',
          cid: '',
          pagenum: 1,
          pagesize: 10
        },
        goodsList: [],
        total: 0,
        // 开启节流阀
        loading: false
        
      }
    },
    onLoad(options) {
      // console.log(options)
      this.queryObj.query = options.query || ''
      this.queryObj.cid = options.cid || ''
      this.getGoodsList()
    },
    methods: {
      // 获取商品列表数据
      async getGoodsList(cb) {
        // 开启节流阀
        this.loading = true
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/goods/search',this.queryObj)
        console.log(res)
        // 在请求结束之后  无论成功与否   关闭节流阀
        this.loading = false
        // 只要数据请求完毕，就立即按需调用 cb 回调函数
        cb && cb()
        if (res.meta.status !== 200) return uni.$showMsg()
        // this.goodsList = res.message.goods
        // 对旧 新数据进行拼接
        this.goodsList = [...this.goodsList,...res.message.goods]
        this.total = res.message.total
      },
      // 跳转到详情页
      gotoDetail(item) {
        uni.navigateTo({
          url:'/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
        })
      }
    },
    // 和methods平级
    onReachBottom() {
      // 判断是否还有数据
      if(this.queryObj.pagenum * this.queryObj.pagesize >= this.total) return uni.$showMsg('数据加载完毕')
      // 对节流阀进行判断
      if(this.loading) return
      // 让页面 自增加一
      this.queryObj.pagenum++
      this.getGoodsList()
    },
    // 下拉刷新
    onPullDownRefresh() {
      this.goodsList = []
      this.total = 0
      this.queryObj.pagenum = 1
      this.loading = false
      this.getGoodsList(() => uni.stopPullDownRefresh())
    }
  }
</script>

<style lang="scss">

</style>
